package com.vir.ai.admin.utils;

import cn.hutool.json.JSONObject;
import com.vir.ai.admin.controller.vo.project.JwtUser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

 
public class SecurityUtils {

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

  
    public static String getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            /*  if (principal instanceof JwtUser) {
                return (JwtUser) principal;
            }*/
            JSONObject jsonObject = new JSONObject(principal);
            Object claims = jsonObject.get("claims");
            JSONObject claimsJsonObject2 = new JSONObject(claims);
            return claimsJsonObject2.get("userId").toString();
        }
        return null;
    }
 

}
